/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fivekage;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author hartono
 */
public class DBHelper {
     private Connection mConn;
     private Statement mStatement ;
     
    public DBHelper() throws SQLException, ClassNotFoundException  {
        makeConnection();
        
        File file = new File("");
        System.out.println("exist "+file.exists() + " "+file.getAbsolutePath());
    }           
    
    private void makeConnection() throws SQLException, ClassNotFoundException{
        Class.forName("com.mysql.jdbc.Driver");
        mConn = DriverManager.getConnection("jdbc:mysql://localhost/fivekage", "progin", "progin");
        mStatement = mConn.createStatement();        
    }
    
    public void closeConnection() throws SQLException {
        mConn.close();
    }
    
//    
//    public void getAllUsers() throws SQLException{
//        Statement statement =mConn.createStatement();
//        ResultSet results = statement.executeQuery("select * from `tbl_user`;");            
//        int i=0;
//        while (results.next()){
//            System.out.println(results.getInt(1) + " "+results.getString(2));            
//            i++;
//        }
//        results.close();
//        statement.close();
//        System.out.println("User count "+i);
//    }
    
    public void getTables() throws SQLException{    
        ResultSet results = mStatement.executeQuery("show tables;");            
        int i=0;
        while (results.next()){
            System.out.println(results.getString(1));            
            i++;
        }
        results.close();        
        System.out.println("Table count "+i);
    }
    
    public String getTablesS()throws SQLException{
        StringBuilder sb = new StringBuilder();        
        ResultSet results = mStatement.executeQuery("show tables;");            
        int i=0;
        while (results.next()){
            System.out.println(results.getString(1));            
            sb.append(results.getString(1));
            sb.append('\n');
            i++;
        }
        results.close();        
        System.out.println("Table count "+i);
        return sb.toString();
    }
    
    
    
    
    public boolean insertDataSet(String fileName){
        try{            
            String query = "insert into tbl_file  values ( '"+fileName +"') ;";                        
            System.out.println(query);
            return mStatement.executeUpdate(query)!=0;            
        }catch(Exception e){
            
            return false;
        }               
    }
    
    public static void main (String args[]) throws SQLException, ClassNotFoundException{
        DBHelper db = new DBHelper();
        System.out.println(db.getTablesS());
    }
            
}
